package com.oj.friend.elasticsearch;

import com.oj.friend.domain.question.es.QuestionES;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
 * ClassName: QuestionRepository
 * Package: com.oj.friend.elasticsearch
 * Description:
 *
 * @Author 宋斌
 * @Create 2025/6/9 22:38
 * @Version 1.0
 */
@Repository
public interface QuestionRepository extends ElasticsearchRepository<QuestionES, Long> {

    //自动根据函数名字生成代码
    Page<QuestionES> findQuestionByDifficulty(Integer difficulty, Pageable pageable);

    @Query("{\"bool\": { " +
            "  \"must\": [ " +
            "    { \"term\": { \"difficulty\": ?2 } }, " +  // 必须匹配 difficulty
            "    { \"bool\": { " +
            "      \"should\": [ " +
            "        { \"match\": { \"title\": \"?0\" } }, " +  // title 匹配
            "        { \"match\": { \"content\": \"?1\" } } " +  // content 匹配
            "      ], " +
            "      \"minimum_should_match\": 1 " +  // 至少匹配 should 里的一个条件
            "    }} " +
            "  ] " +
            "}}")
    Page<QuestionES> findByTitleOrContentAndDiffulty(String keyWordTitle, String keyWordContent, Integer difficulty, Pageable pageable);

    @Query("{\"bool\": {\"should\": [ " +
            "{\"match\": {\"title\": \"?0\"}}, " +
            "{\"match\": {\"content\": \"?1\"}} " +
            "]}}")
    Page<QuestionES> findByTitleOrContent(String keyWordTitle, String keyWordContent, Pageable pageable);
}
